package common

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Result struct {
	Ctx *gin.Context
}

type ResultVo struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func NewResult(ctx *gin.Context) *Result {
	return &Result{Ctx: ctx}
}

func (r *Result) Success(data interface{}, msg ...string) {
	if data == nil {
		data = gin.H{}
	}
	res := ResultVo{}
	res.Code = 200
	if len(msg) <= 0 {
		res.Msg = "success"
	} else {
		res.Msg = msg[0]
	}
	res.Data = data
	r.Ctx.JSON(http.StatusOK, res)
}

// 出错
func (r *Result) Error(code int, msg string) {
	res := ResultVo{}
	res.Code = code
	res.Msg = msg
	r.Ctx.JSON(http.StatusOK, res)
}
